﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
using System.Xml;

namespace IMeiWeiWebService
{
    public static class JsonHelper
    {
        /// <summary>
        /// 构造Json  序列化
        /// </summary>
        /// <returns></returns>
        public static JavaScriptSerializer GenJSSerializer(HttpContext Context)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            Context.Response.Clear();
            Context.Response.ContentType = "application/json; charset=utf-8";
            return js;
        }

        public static object DeserializeJson(string jsonString, Type targetType)
        {
            var converters = new List<JsonConverter>();
            converters.Add(new DateTimeConverter());
            converters.Add(new IgnoreXmlElementConverter());

            return JsonConvert.DeserializeObject(jsonString, targetType, new JsonSerializerSettings
            {
                Converters = converters
            });
        }

        public static T DeserializeJson<T>(string jsonString)
        {
            return (T)DeserializeJson(jsonString, typeof(T));
        }

        public static T CopyTo<T>(this object source)
        {
            if (source == null)
            {
                return default(T);
            }

            string json = JsonConvert.SerializeObject(source);
            return JsonConvert.DeserializeObject<T>(json);
        }
    }

    public class IgnoreXmlElementConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return (objectType == typeof(XmlElement[]) || objectType == typeof(XmlElement));
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            reader.Skip();

            return null;
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {

        }
    }

    public class DateTimeConverter : JsonConverter
    {
        private long InitialDateTimeTicks = (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).Ticks;

        public override bool CanConvert(Type objectType)
        {
            return (objectType == typeof(DateTime?) || objectType == typeof(DateTime));
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.Value == null)
            {
                if (objectType == typeof(DateTime?))
                {
                    return null;
                }
                else
                {
                    return DateTime.MinValue;
                }
            }
            else
            {
                var timeValue = reader.Value as DateTime?;

                if (null != timeValue)
                {
                    return (DateTime)timeValue;
                }
                else
                {
                    DateTime utcDateTime = new DateTime(((long)reader.Value) * 10000 + InitialDateTimeTicks, DateTimeKind.Utc);

                    return (DateTime)utcDateTime;
                }
            }
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {

        }

    }
}